//字段下拉框变化
function fieldChange()
{
	var fieldSelect = document.getElementById("fieldSelect");
	var fieldValue = fieldSelect.options[fieldSelect.selectedIndex].value;
	var type = parseField(fieldValue,4);	//字段类型:参数
	var typeCode = type.substr(0,1);		//类型编号
	
	//dk 修改
	var flag = type.substr(2,10);
	//alert(flag);
	// 如果不为空并且不等于0  则代表是字典项
	
	if (flag.length>0){
		if( flag!="0") {
			typeCode="9";
			//alert("字典项");
		}else {
			//alert("非字典项1");
		}
	}else {
		//alert("非字典项2");
		
	}
	if (type.length>5) typeCode="9";

	if (typeCode=="9")				//代码
	{
		document.getElementById("help").style.display="inline";
		document.getElementById("help").onclick=function(){showCode(type.substr(2),'content','contentCode');}
		document.getElementById("content").readOnly=true;
	}
	else
	{
		document.getElementById("help").style.display="none";
		document.getElementById("content").readOnly=false;
	}
	document.getElementById("content").value="";
	document.getElementById("contentEnd").style.display="none";
	document.getElementById("contentEnd").value="";
	initLogic(typeCode);	//初始化关系下拉框
}

//解析下拉框值 1:中文字段 2:英文字段 3:类型参数
//解析下拉框值 1:是否代码显示 2:中文字段 3:英文字段 4:类型参数
function parseField(field,flag)
{
	var result;
	var _pos = field.indexOf("-");
	var pos = field.indexOf("-");
	
	
	switch (flag)
	{
		case 1:
			result = field.substring(0,_pos);
			break;
		case 2:
			result = field.substring(_pos+1,pos);
			break;
		case 3:
			result = field.substr(pos+1);
			break;
	}
	
	//20201118 调整解析方式，上列代码将无效 
	result = "";
	var arrField=field.split("￥");
	if (arrField.length>=flag){
		result = arrField[flag-1];
	}
	
	return result;
}

//弹出代码选择框图
function showCode(classNo,nameId,codeId)
{
	var name=document.getElementById(nameId).value;
	var code=document.getElementById(codeId).value;
	var obj=new Object();
	obj.name=name;
	obj.code=code;
	
	var url="codeServlet?classNo="+classNo +"&selectParent=true";
	var style="dialogWidth:300px;dialogHeight:400px;"
		
		/*
	var results=window.showModalDialog(url,obj,style);
	if (results)
	{
		document.getElementById(nameId).value=results[0];
		document.getElementById(codeId).value=results[1];
	}
		*/
		
	artDialog.data('nameid',nameId);
	artDialog.data('codeid',codeId);
	
	art.dialog.open(url);
}

//加入选择条件
function addCondition()
{
	var sql = document.getElementById("sql");
	var option = document.createElement("option");
	
	var fieldSelect = document.getElementById("fieldSelect");
	var fieldValue = fieldSelect.options[fieldSelect.selectedIndex].value;
	var codesearch = parseField(fieldValue,1);	//是否代码检索字段
	var name = parseField(fieldValue,2);	//中文字段 
	var field = parseField(fieldValue,3);	//英文字段
	var type = parseField(fieldValue,4).substr(0,1);	//字段类型码
	
	if (fieldValue.length>5) type="9";
	
	var logic = document.getElementById("logic");
	var logicEn = logic.options[logic.selectedIndex].value;		//关系符号
	var logicCn = logic.options[logic.selectedIndex].text;		//关系描述
	
	var content = document.getElementById("content").value;
	while (content.indexOf("'")>-1)
	{
		content = content.replace("'","");
	}
	var contentCode = document.getElementById("contentCode").value;
	var contentEnd = document.getElementById("contentEnd").value;
	
	if (logic.selectedIndex!=4)
	{
		option.text = name + " " + logicCn + " " + (content==""?"空":content);
	}
	else
	{
		if (content=="" || contentEnd=="")
		{
			//dialog("温馨提示","请输入查询范围",260,60);
			alert("请输入查询范围");
			return;
		}
		option.text = name + " " + logicCn + " (" + content + "," + contentEnd + ")";
	}
	
	if (type=="0")	//字符串
	{
		if (content=="")
		{
			switch (logic.selectedIndex)
			{
				case 0:
					option.value = "(" + field + " is null or " + field + "=' ')";break;
				case 1:
					option.value = "(" + field + " is not null and " + field + "<>' ')";break;
				case 2:
					option.value = "(" + field + " is null or " + field + "=' ')";break;
			}
		}
		else
		{
			if (field=="spellIndex") 
			{
				content = content.toUpperCase();
			}
			switch (logic.selectedIndex)
			{
				case 0:
					option.value = field + " " + logicEn + " '" + content + "'";break;
				case 1:
					option.value = "(" + field + " " + logicEn + " '" + content + "' or " + field + " is null)";break;
				case 2:
					option.value = field + " " + logicEn + " '%" + content + "%'";break;
			}
		}
	}
	else if ((type=="1") || (type=="2"))	//数值
	{
		if (content=="")
		{
			switch (logic.selectedIndex)
			{
				case 0:
					content = 0;break;
				case 1:
					content = 0;break;
				case 2:
				case 3:
					//dialog("温馨提示","数字格式非法",260,60);
					alert("数字格式非法");
					return;
					break;
			}
		}
		else
		{
			if (!isNumeric(content))
			{
				//dialog("温馨提示","数字格式非法",260,60);
				alert("数字格式非法");
				return;
			}
		}
		option.value = field + logicEn + (content);
	}
	else if(type=="3")	//日期
	{
		if (content=="")
		{
			switch (logic.selectedIndex)
			{
				case 0:
					option.value = "(" + field + " is null or " + field + "=' ')";break;
				case 1:
					option.value = "(" + field + " is not null and " + field + "<>' ')";break;
				case 2:
				case 3:
					//dialog("温馨提示","日期格式非法",260,60);
					alert("日期格式非法");
					return;
					break;
				case 4:
					//dialog("温馨提示","开始日期格式非法",260,60);
					alert("开始日期格式非法");
					break;
			}
		}
		else
		{
			if (!isDate(content))
			{
				//dialog("温馨提示","日期格式非法",260,60);
				alert("日期格式非法");
				return;
			}
			switch (logic.selectedIndex)
			{
				case 0:
					option.value = field + " " + logicEn + " '" + content + "'";break;
				case 1:
					option.value = "(" + field + " " + logicEn + " '" + content + "' or " + field + " is null)";break;
				case 2:
				case 3:
					option.value = field + " " + logicEn + " '" + content + "'";break;
				case 4:
					if (!isDate(contentEnd))
					{
						//dialog("温馨提示","结束日期格式非法",260,60);
						alert("结束日期格式非法");
						return;
					}
					option.value = "(" + field + " between '" + content + "' and '" + contentEnd +  "')";break;
			}
		}
	}
	else if(type=="9")	//代码
	{
		if (content=="")
		{
			switch (logic.selectedIndex)
			{
				case 0:
					option.value = "(" + field + " is null or " + field + "=' ')";break;
				case 1:
					option.value = "(" + field + " is not null and " + field + "<>' ')";break;
				case 2:
					option.value = "(" + field + " is null or " + field + "=' ')";break;
			}
		}
		else
		{
			//20201128 根据字段是否代码显示值组合条件
			if ((codesearch=="F")||(codesearch=="否")){
				switch (logic.selectedIndex)
				{
					case 0:
						option.value = field + " " + logicEn + " '" + content + "'";break;
					case 1:
						option.value = "(" + field + " " + logicEn + " '" + content + "' or " + field + " is null)";break;
					case 2:
						option.value = field + " " + logicEn + " '%" + content + "%'";break;
				}
			}else{
				switch (logic.selectedIndex)
				{
					case 0:
						option.value = field + " " + logicEn + " '" + contentCode + "'";break;
					case 1:
						option.value = "(" + field + " " + logicEn + " '" + contentCode + "' or " + field + " is null)";break;
					case 2:
						option.value = field + " " + logicEn + " '" + contentCode + "%'";break;
				}
			}
		}
	}
	sql.options.add(option);
}

//删除选中条件
function deleteCondition()
{
	var sql = document.getElementById("sql");
	if (sql.selectedIndex>-1)
	{
		sql.options.remove(sql.selectedIndex);
	}
}

//确认条件,发送查询请求
function ensureCondition()
{
	var sqlResult="";
	var andor = document.getElementById("and").checked?" and " : " or  ";
	var sql = document.getElementById("sql");
	for (var i=0;i<sql.options.length;i++)
	{
		sqlResult += andor + sql.options[i].value;
	}
	sqlResult = sqlResult.substr(5);
	return sqlResult;
}

function getSearchName()
{
	var searchName = document.getElementById("searchName").value;
	return searchName;
}

//初始化高级查询界面
function initQuery()
{
	var fieldSelect = document.getElementById("fieldSelect");
	fieldSelect.selectedIndex=0;	//字段下拉框定位第一元素
	var type = parseField(fieldSelect.options[fieldSelect.selectedIndex].value,4);
	initLogic(type.substr(0,1));
	
	document.getElementById("help").style.display="none";
	document.getElementById("content").readOnly=false;
	document.getElementById("content").onkeydown="";
	document.getElementById("content").value="";
	document.getElementById("contentCode").value="";
	document.getElementById("contentEnd").style.display="none";
	
	//清空已加入条件
	var sql = document.getElementById("sql");	
	while (sql.options.length>0)
	{
		sql.options.remove(0);
	}
}

//初始化关系下拉框0:字符(等于、不等于、模糊等于) 1,2:数值(等于、不等于、小于、大于) 3:日期(等于、不等于、小于、大于、在...之间)
function initLogic(type)
{
	//清空关系符
	var logic = document.getElementById("logic");
	while (logic.options.length>0)
	{
		logic.options.remove(0);
	}
	
	var oEqual = document.createElement("option");
	oEqual.text = "等于";
	oEqual.value = "=";
	logic.options.add(oEqual);
	
	var oNotEqual = document.createElement("option");
	oNotEqual.text = "不等于";
	oNotEqual.value = "<>";
	logic.options.add(oNotEqual);
	
	if (type=="0" || type=="9")
	{
		var oLike = document.createElement("option");
		oLike.text = "模糊等于";
		oLike.value = "like";
		logic.options.add(oLike);
	}
	if (type=="1" || type=="2" || type=="3")
	{
		var oLT = document.createElement("option");
		oLT.text = "小于";
		oLT.value = "<";
		logic.options.add(oLT);
		
		var oGT = document.createElement("option");
		oGT.text = "大于";
		oGT.value = ">";
		logic.options.add(oGT);
		
		if (type=="3")//时间类型
		{
			var oBetween  = document.createElement("option");
			oBetween.text = "在...之间";
			oBetween.value = "between and";
			logic.options.add(oBetween);
		}
	}
}

function isDate(str)
{
	var result=false;
	if (isNumeric(str)) 
	{
		switch (str.length)
		{
			case 4:
				var year=str;
				var d=new Date(year,'01','01');
				if (d.getFullYear()==year)
				{
					result=true;
				}
				break;
			case 6:
				var year=str.substr(0,4);
				var month=str.substr(4,2)-1;
				var d=new Date(year,month,'01');
				if (d.getFullYear()==year && d.getMonth()==month)
				{
					result=true;
				}
				break;
			case 8:
				var year=str.substr(0,4);
				var month=str.substr(4,2)-1;
				var day=str.substr(6,2);
				var d=new Date(year,month,day);
				if ((d.getFullYear()==year) && (d.getMonth()==month) && (d.getDate()==day))
				{
					result=true;
				}
				break;
		}
	}
	
	return result;
}

function isNumeric(str)
{
	var result=false;
	var regWord = /^[0-9]+$/;
	if (regWord.test(str)) 
	{
		result=true;
	}
	
	return result;
}

function logicChange()
{
	var logic = document.getElementById("logic");
	switch (logic.selectedIndex)
	{
		case 0:
		case 1:
		case 2:
		case 3:
			document.getElementById("contentEnd").style.display="none";
			break;
		case 4:
			document.getElementById("contentEnd").style.display="inline";
			break;
	}
}
